package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhentao.mapper.*;
import com.zhentao.model.entity.product.Product;
import com.zhentao.model.entity.product.ProductDetails;
import com.zhentao.model.entity.product.ProductSku;
import com.zhentao.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Override
    public List< Product > findAll() {
        return productMapper.selectList( null );
    }

    @Override
    public List< Product > selectByName( String name ) {
        return productMapper.selectList( Wrappers.< Product > lambdaQuery().like( StringUtils.isNotBlank( name ), Product::getName, name ) );
    }

    @Override
    public List< Product > selectByParentCategory( Integer id ) {
        return productMapper.selectList( Wrappers.< Product > lambdaQuery().like( id != null, Product::getCategory1Id, id ) );
    }

    @Override
    public List< Product > selectBySonCategory( Integer id ) {
        return productMapper.selectList( Wrappers.< Product > lambdaQuery().like( id != null, Product::getCategory2Id, id ) );
    }

    @Override
    public List< Product > selectBySonCategory2( Integer id ) {
        return productMapper.selectList( Wrappers.< Product > lambdaQuery().like( id != null, Product::getCategory3Id, id ) );
    }

    @Override
    public Product findById( Long id ) {
        Product product = productMapper.selectById( id );
        product.setCategory1Name( categoryMapper.selectById( product.getCategory1Id() ).getName() );
        product.setCategory2Name( categoryMapper.selectById( product.getCategory2Id() ).getName() );
        product.setCategory3Name( categoryMapper.selectById( product.getCategory3Id() ).getName() );
        product.setBrandName( brandMapper.selectById( product.getBrandId() ).getName() );
        product.setProductSkuList( productSkuMapper.selectList( Wrappers.< ProductSku > lambdaQuery().eq( ProductSku::getProductId, id ) ) );
        product.setDetailsImageUrls( productDetailsMapper.selectOne( Wrappers.< ProductDetails > lambdaQuery().eq( ProductDetails::getProductId, id ) ).getImageUrls() );
        return product;
    }


}
